// fetchHourlyData
import dayjs from "dayjs";

export default function ({ arrangedData, arrangedPlanData, icaoCodes }) {
  const nowMoment = dayjs();
  const tableHourlyData = new Array();
  const tableHourlyHeader = [
    {
      prop: "col_0",
      label: "统计目标1",
      column: "-1",
    },
    {
      prop: "col_1",
      label: "统计目标2",
      column: "-1",
    },
    {
      prop: "col_2",
      label: "数据参考",
      column: "-1",
    },
  ];
  const sumLineRealDep = { col_0: "终端区", col_1: "起飞", col_2: "实时" };
  const sumLineRealArr = { col_0: "终端区", col_1: "落地", col_2: "实时" };
  const sumLineRealTot = { col_0: "终端区", col_1: "合计", col_2: "实时" };
  const sumLinePlanDep = { col_0: "终端区", col_1: "起飞", col_2: "计划" };
  const sumLinePlanArr = { col_0: "终端区", col_1: "落地", col_2: "计划" };
  const sumLinePlanTot = { col_0: "终端区", col_1: "合计", col_2: "计划" };

  // sectorRequire
  // extInfo.standalone
  const extSectorPlan = { col_0: "席位数", col_1: "建议", col_2: "计划" };
  const extSectorReal = { col_0: "席位数", col_1: "建议", col_2: "实时" };
  const extSector = { col_0: "席位数", col_1: "实际", col_2: "-" };
  const extSuper = { col_0: "带班数", col_1: "实际", col_2: "-" };
  const extIpiaPlan = { col_0: "独立进近", col_1: "建议", col_2: "计划" };
  const extIpiaReal = { col_0: "独立进近", col_1: "建议", col_2: "实时" };
  const extIpia = { col_0: "独立进近", col_1: "实际", col_2: "-" };
  const extDirUU = { col_0: "双流方向", col_1: "实际", col_2: "-" };
  const extDirTF = { col_0: "天府方向", col_1: "实际", col_2: "-" };

  icaoCodes.forEach((icao, j) => {
    const lineRealDep = { col_0: icao, col_1: "起飞", col_2: "实时" };
    const lineRealArr = { col_0: icao, col_1: "落地", col_2: "实时" };
    const lineRealTot = { col_0: icao, col_1: "合计", col_2: "实时" };
    const linePlanDep = { col_0: icao, col_1: "起飞", col_2: "计划" };
    const linePlanArr = { col_0: icao, col_1: "落地", col_2: "计划" };
    const linePlanTot = { col_0: icao, col_1: "合计", col_2: "计划" };
    arrangedData.forEach((dataReal, i) => {
      // 确定静态信息
      const isAfter = nowMoment.isAfter(dataReal.timestamp);
      const identity = "pek_" + dayjs(dataReal.timestamp).format("MMDD_HHmm");
      const dataPlan = arrangedPlanData[i];

      // 生成表头
      if (j == 0) {
        tableHourlyHeader.push({
          prop: identity,
          label: dayjs(dataReal.timestamp).format("HH") + "时",
          column: i,
        });
      }

      // ext 信息

      // 席位建议
      extSectorPlan[identity] = dataPlan.seatAdviceAll ?? 0;
      extSectorReal[identity] = isAfter ? dataReal.seatAdviceAll ?? 0 : "";
      // 席位实际
      extSector[identity] = isAfter ? dataReal.real.seats ?? 0 : "";
      extSuper[identity] = isAfter ? dataReal.real.super ?? "" : "";

      // 独立进近
      extIpiaPlan[identity] = dataPlan.extInfo
        ? dataPlan.extInfo.standalone
          ? "√"
          : ""
        : "";
      extIpiaReal[identity] = dataReal.extInfo
        ? dataReal.extInfo.standalone
          ? "√"
          : ""
        : "";
      // 独立进近
      extIpia[identity] = isAfter ? (dataReal.real.ipia ? "√" : "") : "";

      // 运行方向
      extDirUU[identity] = isAfter ? dataReal.real.dirUU ?? "" : "";
      extDirTF[identity] = isAfter ? dataReal.real.dirTF ?? "" : "";

      // 机场实时数据
      lineRealDep[identity] = isAfter
        ? dataReal.airport[icao].type.DEP.total
        : "";
      lineRealArr[identity] = isAfter
        ? dataReal.airport[icao].type.ARR.total
        : "";
      lineRealTot[identity] = isAfter ? dataReal.airport[icao].total : "";

      // 机场计划数据

      linePlanDep[identity] = dataPlan.airport[icao].type.DEP.total;
      linePlanArr[identity] = dataPlan.airport[icao].type.ARR.total;
      linePlanTot[identity] = dataPlan.airport[icao].total;

      // 汇总行数据累加：计划数据

      sumLinePlanDep[identity] = sumLinePlanDep[identity]
        ? sumLinePlanDep[identity] + linePlanDep[identity]
        : linePlanDep[identity];

      sumLinePlanArr[identity] = sumLinePlanArr[identity]
        ? sumLinePlanDep[identity] + linePlanArr[identity]
        : linePlanArr[identity];

      sumLinePlanTot[identity] = sumLinePlanTot[identity]
        ? sumLinePlanTot[identity] + linePlanTot[identity]
        : linePlanTot[identity];

      // 汇总行数据累加：实时数据

      sumLineRealDep[identity] = isAfter
        ? sumLineRealDep[identity]
          ? sumLineRealDep[identity] + lineRealDep[identity]
          : lineRealDep[identity]
        : "";

      sumLineRealArr[identity] = isAfter
        ? sumLineRealArr[identity]
          ? sumLineRealDep[identity] + lineRealArr[identity]
          : lineRealArr[identity]
        : "";

      sumLineRealTot[identity] = isAfter
        ? sumLineRealTot[identity]
          ? sumLineRealTot[identity] + lineRealTot[identity]
          : lineRealTot[identity]
        : "";
    });
    tableHourlyData.push(
      linePlanDep,
      lineRealDep,
      linePlanArr,
      lineRealArr,
      linePlanTot,
      lineRealTot
    );
  });

  tableHourlyData.push(
    sumLinePlanDep,
    sumLineRealDep,
    sumLinePlanArr,
    sumLineRealArr,
    sumLinePlanTot,
    sumLineRealTot,
    // ext info
    extSectorPlan,
    extSectorReal,
    extSector,
    extSuper,
    extIpiaPlan,
    extIpiaReal,
    extIpia,
    extDirUU,
    extDirTF
  );

  return { tableHourlyHeader, tableHourlyData };
}
